using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.ServiceModel.Syndication;
using AmazonCommon;
using System.Xml;
using AmazonCommon.Amazon.ECS;
using DDNY.WebPortals.Common;

namespace AmazonVer3.Controllers
{
    public class FeedController : ApplicationController
    {
        public ActionResult TopSellers(string id)
        {
            string[] idItems = id.Split(new char[] { '-' });
            string searchIndex = idItems[1];
            string nodeid = idItems[0];
            Items topsellers = AmazonHelper.GetTopSellersForNode(nodeid, searchIndex);
            if (topsellers != null && topsellers.Item != null)
            {


                string currentPortalUrl = HttpContext.Request.Url.Authority;
                currentPortalUrl = "http://" + currentPortalUrl;
                SyndicationFeed feed =
           new SyndicationFeed("Top Sellers",
                               "Top Sellers for Browse Node " + nodeid,
                               new Uri(currentPortalUrl + "/Feed/TopSellers/" + id),
                               "TopSellers" + id,
                               new DateTimeOffset(DateTime.Now));
                List<SyndicationItem> items = new List<SyndicationItem>();
                foreach (var productItem in topsellers.Item)
                {

                    SyndicationItem item =
                        new SyndicationItem(productItem.ItemAttributes.Title,
                            productItem.ItemAttributes.Title,
                            new Uri(currentPortalUrl + "/Product/ShowProductDetails/" + productItem.ASIN));

                    items.Add(item);
                }
                feed.Items = items;
                return new RssActionResult() { Feed = feed };
            }
            return new PermanentRedirectResult("/");


        }


        public ActionResult Categories()
        {
            string currentPortalUrl = HttpContext.Request.Url.Authority;
            currentPortalUrl = "http://" + currentPortalUrl;
            SyndicationFeed feed =
       new SyndicationFeed("All Categories",
                           "This feed includes all categories",
                           new Uri(currentPortalUrl + "/Feed/Categories"),
                           "Categories",
                           new DateTime(2009, 10, 1));

            List<SearchIndexByLocale> indices = ViewData[VDConstants.SearchIndices] as List<SearchIndexByLocale>;
            List<SyndicationItem> items = new List<SyndicationItem>();
            foreach (var searchIndexItem in indices)
            {

                SyndicationItem item =
                    new SyndicationItem(searchIndexItem.SearchIndex.IndexName,
                        searchIndexItem.SearchIndex.IndexName,
                        new Uri(currentPortalUrl + "/Product/List/" + searchIndexItem.BrowseNodeAmazonID + "-" + searchIndexItem.SearchIndex.IndexName));

                items.Add(item);
            }
            feed.Items = items;
            return new RssActionResult() { Feed = feed };
        }

        public class RssActionResult : ActionResult
        {
            public SyndicationFeed Feed { get; set; }

            public override void ExecuteResult(ControllerContext context)
            {
                context.HttpContext.Response.ContentType = "application/rss+xml";

                Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(Feed);
                using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
                {
                    rssFormatter.WriteTo(writer);
                }
            }
        }

    }
}
